'use strict';

module.exports = app => {
    return class KnowledgeService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }


        /**
         * 获取教材
         * @returns {Promise<void>}
         */
        async getMaterial({ courseId }) {
            let url = this.app.urls('getMaterial');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    courseId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;

        }

        /**
         * 获取章节
         * @returns {Promise<void>}
         */
        async getCharpter({ materialId }) {
            let url = this.app.urls('getCharpter');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    materialId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;

        }

        /**
         * 获取知识点
         * @returns {Promise<void>}
         */
        async point({ courseId }) {
            let url = this.app.urls('pointList');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    courseId
                },
                timeout: 10000
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        // /**
        //  * 获取知识点
        //  * @returns {Promise<void>}
        //  */
        // async point({ courseId, type }) {
        //     let material = await this.getMaterial({ courseId });
        //
        //     let point = [];
        //
        //     for (let i = 0; i < material.length; i++){
        //         let item = material[i];
        //
        //         let book = item.term == 1 ? "上册" : "下册";
        //
        //         let term = `${item.gradeName}${book}`;
        //
        //         //获取 charpter
        //         let charpter = await this.getCharpter({
        //             materialId: item.id
        //         });
        //
        //         let children = [];
        //
        //         this.getChildren(children, charpter, "#");
        //
        //         // console.log(children);
        //
        //         point.push({
        //             id: item.id,
        //             label: term,
        //             children
        //         });
        //     }
        //
        //     return point;
        // }


        getChildren(children, list, parentId){
            for (let i = 0; i < list.length; i++){
                let item = list[i];
                if (parentId == item.parent){

                    let sub = [];
                    this.getChildren(sub, list, item.id);
                    let leaf = {
                        id: item.id,
                        label: item.text,
                        icon: item.icon,
                        children: sub
                    };

                    children.push(leaf);
                }
            }
        }



    }

};
